package com.kaizzi.data.ppt.template;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.common.been.NameDouble;
import com.kaizzi.data.common.been.SeriesData;
import com.kaizzi.data.jdl.service.LogService;
import com.kaizzi.data.ppt.TemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xslf.usermodel.XSLFChart;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/27
 */
@Slf4j
@Component
public class Template21 {

    @Resource
    private TemplateUtil templateUtil;
    @Resource
    private LogService logService;

    public void text(List<XSLFTextParagraph> textParagraphs,JsonNode data) {
        if(textParagraphs==null||textParagraphs.size()==0){
            return;
        }
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            if(textParagraph==null){
                continue;
            }
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            if(textRuns==null){
                continue;
            }
            for (XSLFTextRun textRun : textRuns) {
                if (textRun == null || textRun.getXmlObject()  == null){
                    return;
                }
                try {
                    if (textRun.getRawText().equals("21KEY-1")){
                        String title = data.get("title").asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("21KEY-3")){
                        String s = data.get("utcTitle").asText();
                        templateUtil.text(textParagraph,s,false);
                        break;
                    }
                    if (textRun.getRawText().equals("21KEY-5")){
                        String s = data.get("synTitle").asText();
                        templateUtil.text(textParagraph,s,false);
                        break;
                    }
                    if (textRun.getRawText().equals("21KEY-7")){
                        String dataPeriod = data.get("dataPeriod").asText();
                        textRun.setText(dataPeriod);
                        break;
                    }
                }catch (Exception e){
                    logService.setDataLogStatus("ppt-error");
                    log.error("page:{},text exception:{}",21,e);
                }
            }
        }
    }

    public void barChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
        List<SeriesData> data1 = getData(data);
        templateUtil.barChart(plot,data1,workbook,sheet,chart);
    }

    private static List<SeriesData> getData(JsonNode data){
        JsonNode jsonNode = data.get("utcChannel");
        List<SeriesData> seriesDatas = new ArrayList<>();
        List<NameDouble> tempDouble = new ArrayList<>();

        for (JsonNode dateJson:jsonNode) {
            NameDouble playerDouble = new NameDouble(dateJson.get("utcName").asText(),dateJson.get("utcCount").asDouble());
            tempDouble.add(playerDouble);
        }

        SeriesData playerSeriesData = new SeriesData("",tempDouble.stream().sorted(Comparator.comparing(NameDouble::getValue).reversed()).collect(Collectors.toList()), "");
        seriesDatas.add(playerSeriesData);

        return seriesDatas;
    }

    public void pieChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
        List<SeriesData> data1 = getPieData(data);
        templateUtil.pieChart(plot,data1,workbook,sheet,chart);
    }

    private static List<SeriesData> getPieData(JsonNode data){
        JsonNode jsonNode = data.get("syns");
        List<SeriesData> seriesDatas = new ArrayList<>();
        List<NameDouble> tempDouble = new ArrayList<>();

        for (JsonNode dateJson:jsonNode) {
            NameDouble playerDouble = new NameDouble(dateJson.get("synName").asText(),dateJson.get("synCount").asDouble());
            tempDouble.add(playerDouble);
        }

        SeriesData playerSeriesData = new SeriesData("",tempDouble.stream().sorted(Comparator.comparing(NameDouble::getValue).reversed()).collect(Collectors.toList()), "");
        seriesDatas.add(playerSeriesData);

        return seriesDatas;
    }

}
